﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TI_4_Limonade_Fabriek.Database;
using TI_4_Limonade_Fabriek.Model;
using TI_4_Limonade_Fabriek.Properties;

namespace TI_4_Limonade_Fabriek
{
    public partial class EditBatchScreen : Form
    {
        Batch batch;
        BatchDBController bdbc;

        public EditBatchScreen(Batch b)
        {
            InitializeComponent();
            batch = b;
            bdbc = new BatchDBController();
            FillFormWithData();
            BatchDatePicker.MinDate = DateTime.Now;
            
        }

        private void FillFormWithData()
        {
            tbBatchNaam.Text = batch.Naam;
            nudAantalBekers.Value = batch.AantalBekers;
            nudMengverhoudingPercentage.Value = batch.Mengverhouding;
            cbKleur.SelectedItem = batch.Kleur;
            nudPrioriteit.Value = batch.Prioriteit;
            BatchDatePicker.SelectionStart = DateTime.Parse(batch.Datum);
        }

        private void BatchBewerken_Load(object sender, EventArgs e)
        {
            LoadNUDs();
        }

        private void LoadNUDs()
        {
            nudMengverhoudingPercentage.Minimum = Settings.Default.MinMengverhouding;
            nudMengverhoudingPercentage.Maximum = Settings.Default.MaxMengverhouding;
            nudMengverhoudingPercentage.Increment = Settings.Default.MengverhoudingStappen;
            nudAantalBekers.Maximum = Settings.Default.AantalBekers;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            batch.Status = 0;
            
            bdbc.UpdateBatch(batch);
            this.Dispose();
        }

        private void btnSaveBatch_Click(object sender, EventArgs e)
        {
            CollectData();
            batch.Status = 0;
            bdbc.UpdateBatch(batch);
            this.Dispose();

        }

        private void CollectData()
        {
            batch.Naam = tbBatchNaam.Text.ToString();
            batch.AantalBekers = int.Parse(nudAantalBekers.Value.ToString());
            batch.Mengverhouding = int.Parse(nudMengverhoudingPercentage.Value.ToString());
            batch.Kleur = cbKleur.SelectedItem.ToString();
            batch.Prioriteit = int.Parse(nudPrioriteit.Value.ToString());
            batch.Datum = BatchDatePicker.SelectionStart.ToString("yyyy-MM-dd");

        }

        private void EditBatchScreen_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
        }
    }
}
